package cn.bdqn.converter;

import org.springframework.core.convert.converter.Converter;

import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDateConverter implements Converter<String, Date> {

    private SimpleDateFormat[] dateFormats = {
            new SimpleDateFormat("yyyy-MM-dd"),
            new SimpleDateFormat("yyyy/MM/dd"),
            new SimpleDateFormat("yyyy年MM月dd日")
    };

    @Override
    public Date convert(String s) {
        for (SimpleDateFormat dateFormat : dateFormats) {
            try {
                Date date = dateFormat.parse(s);
                return date;
            }catch (Exception e){
                continue;
            }
        }
        throw new RuntimeException("日期格式错误！");
    }
}
